﻿using DZParallelLib.Test.Lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Threading.Tasks;

namespace DZParallelLib.Test
{
    [TestClass]
    public class LargeCollectionTests
    {
        [TestInitialize]
        public void TestInit()
        {
            UtilsForTest.SetupTest();
        }

        public DirectoryInfo LC(int size)
        {
            return new DirectoryInfo(string.Format(@".\LargeCollection", size));
        }

        public DirectoryInfo CreateLargeCollection(int size, string baseDir = "DZImage1")
        {
            var largeCollection = LC(size);
            if (!largeCollection.Exists)
            {
                var largeCollectionSC = new DirectoryInfo(Path.Combine(largeCollection.FullName, "sc"));
                largeCollectionSC.Create();
                var i1 = new FileInfo(string.Format(@".\{0}\sc\vnpzyair.xml", baseDir));
                for (int i = 0; i < size; i++)
                {
                    var newImage = i1.GenerateNewDZFile(string.Format("vnpzyair{0}.xml", i), largeCollectionSC);
                }
            }
            return largeCollection;
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImageGenericLargeImageRendering")]
        public async Task GenericLargeImageRendering()
        {
            var dir = CreateLargeCollection(100, "DZImageGenericLargeImageRendering");
            var c = new RenderDZCollectionFromImages();

            var output = new FileInfo(Path.Combine(dir.FullName, "dz.xml"));
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var subImageDir = new DirectoryInfo(Path.Combine(dir.FullName, "sc"));

            var rightNow = DateTime.Now;
            var updated = await c.RenderCollection(subImageDir.EnumerateFiles(),
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);
            var delta = DateTime.Now - rightNow;
            Console.WriteLine("Total time to run the test was {0}.", delta);
        }
    }
}
